package org.esgi.transformer.impl;

import java.util.ArrayList;
import java.util.List;

import org.esgi.model.impl.Question;
import org.esgi.model.interfaces.IAnswer;
import org.esgi.model.interfaces.IQuestion;

import org.esgi.transformer.interfaces.IXMLQuestionTransformer;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLQuestionTransformer extends XMLModelTransformer implements IXMLQuestionTransformer
{
	private XMLAnswerTransformer answerTransformer_;
	
	public static final String XMLQuestionNodeName = "question";
	public static final String XMLQuestionIdAttrName = "id";
	public static final String XMLQuestionContentNodeName = "content";
	public static final String XMLQuestionAnswersNodeName = "answers";
	
	public XMLQuestionTransformer(XMLAnswerTransformer answerTransformer)
	{
		answerTransformer_ = answerTransformer;
	}
	
	public IQuestion questionFromElement(Element e)
	{
		Question q = new Question();
		q.setId(e.getAttribute(this.addDefaultNamespaceToName(XMLQuestionIdAttrName)));
		NodeList nl = e.getChildNodes();
		for(int i=0 ; i<nl.getLength(); i++)
		{
			Node childNode = nl.item(i);
			if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLQuestionContentNodeName)))
			{
				Node textNode = childNode.getFirstChild();
				if(textNode != null)
					q.setText(textNode.getNodeValue());
			}
			else if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLQuestionAnswersNodeName)))
			{
				NodeList nlAnswers = childNode.getChildNodes();
				if(nlAnswers != null)
				{
					List<IAnswer> answers = new ArrayList<IAnswer>();
					for(int j=0 ; j<nlAnswers.getLength(); j++)
					{
						Node answerNode = nlAnswers.item(j);
						if(answerNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLAnswerTransformer.XMLAnswerNodeName)))
							answers.add(answerTransformer_.answerFromElement((Element) answerNode));
					}
					q.setAnswers(answers);
				}
			}
		}
		return q;
	}

	public Element questionToElement(IQuestion aQuestion, Document doc)
	{
		if((aQuestion instanceof IQuestion) && doc != null)
		{
			Element questionNode = doc.createElement(this.addDefaultNamespaceToName(XMLQuestionNodeName));
			questionNode.setAttribute(this.addDefaultNamespaceToName(XMLQuestionIdAttrName), aQuestion.getId());
			
			Element contentNode = doc.createElement(this.addDefaultNamespaceToName(XMLQuestionContentNodeName));
			contentNode.appendChild(doc.createTextNode(aQuestion.getText()));
			questionNode.appendChild(contentNode);
			
			Element answersNode = doc.createElement(this.addDefaultNamespaceToName(XMLQuestionAnswersNodeName));
			List<IAnswer> answers = aQuestion.getAnswers();
			if(answers != null)
			{
				for(int i=0; i<answers.size(); i++)
				{					
					Element answerNode = answerTransformer_.answerToElement(answers.get(i), doc);
					if(answerNode != null)
						answersNode.appendChild(answerNode);
				}
			}
			questionNode.appendChild(answersNode);
			return questionNode;
		}
		return null;
	}
}
